package com.tyj.movie.config;

import com.tyj.movie.config.interceptor.CityInterceptor;
import com.tyj.movie.config.interceptor.LoginInterceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 日期: 2021/6/27 11:45
 * 作者: 谭友杰
 * 类说明:
 */
@Component
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("login.html").setViewName("login");
        registry.addViewController("profile.html").setViewName("profile");
        registry.addViewController("personal").setViewName("personal");
        registry.addViewController("orderSuccess").setViewName("orderSuccess");
        registry.addViewController("paySuccess").setViewName("paySuccess");
        registry.addViewController("orderRefund").setViewName("orderRefund");


    }

    /**
     * 处理跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                //针对header单独设置，不然无法获取header中的请求信息，前端也无法拿到响应中的
                // header信息，OPTIONS请求也会经过拦截器，在进行登录拦截时候要注意特殊处理
                .allowedHeaders("*")
                .exposedHeaders("access-control-allow-headers",
                        "access-control-allow-methods",
                        "access-control-allow-origin",
                        "access-control-max-age",
                        "X-Frame-Options")
                .maxAge(3600)
                .allowCredentials(true);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/movie/comment/giveLike/**","/movie/comment/add","/movie/wantSee/*");
        registry.addInterceptor(new CityInterceptor()).addPathPatterns("/**");
    }
}
